ObjectOutlineRendererFeature.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections.Generic;
  2. using ExternPropertyAttributes;
  3. using JetBrains.Annotations;
  4. using UnityEditor;
  5. using UnityEngine;
  6. #if UNITY_2023_3_OR_NEWER
  7. using UnityEngine.Rendering.Universal;
  8. #else
  9. using UnityEngine.Experimental.Rendering.Universal;
  10. #endif
  11. namespace FlatKit {
  12. #if UNITY_EDITOR
  13. [CustomEditor(typeof(ObjectOutlineRendererFeature))]
  14. public class ObjectOutlineRendererFeatureEditor : Editor {
  15. private bool _advancedSettingsFoldout;
  16. public override void OnInspectorGUI() {
  17. var feature = target as ObjectOutlineRendererFeature;
  18. if (feature == null) return;
  19. #if !UNITY_2022_3_OR_NEWER
  20. EditorGUILayout.Space();
  21. EditorGUILayout.HelpBox("This feature requires Unity 2022.3 or newer.", MessageType.Error);
  22. EditorGUILayout.Space(-10);
  23. #else
  24. // Default properties.
  25. _advancedSettingsFoldout = EditorGUILayout.Foldout(_advancedSettingsFoldout, "Advanced Settings");
  26. if (_advancedSettingsFoldout) {
  27. EditorGUI.indentLevel++;
  28. // Style the background of the foldout.
  29. EditorGUILayout.BeginVertical("HelpBox");
  30. base.OnInspectorGUI();
  31. EditorGUILayout.EndVertical();
  32. EditorGUI.indentLevel--;
  33. }
  34. // Custom properties.
  35. var autoReferenceMaterials =
  36. serializedObject.FindProperty(nameof(ObjectOutlineRendererFeature.autoReferenceMaterials));
  37. EditorGUI.indentLevel--;
  38. autoReferenceMaterials.boolValue = EditorGUILayout.ToggleLeft(
  39. new GUIContent(autoReferenceMaterials.displayName, autoReferenceMaterials.tooltip),
  40. autoReferenceMaterials.boolValue);
  41. EditorGUI.indentLevel++;
  42. if (autoReferenceMaterials.boolValue) {
  43. EditorGUI.indentLevel++;
  44. EditorGUI.BeginDisabledGroup(true);
  45. EditorGUILayout.BeginVertical("HelpBox");
  46. var materials = serializedObject.FindProperty(nameof(ObjectOutlineRendererFeature.materials));
  47. EditorGUILayout.PropertyField(materials, true);
  48. EditorGUILayout.EndVertical();
  49. EditorGUI.EndDisabledGroup();
  50. EditorGUI.indentLevel--;
  51. }
  52. #endif
  53. }
  54. }
  55. #endif
  56. public class ObjectOutlineRendererFeature : RenderObjects {
  57. [ReadOnly]
  58. [Tooltip("Materials using this feature. The list is updated automatically based on the `Enable Outline` toggle " +
  59. "on materials using the Stylized Surface shader.")]
  60. [HideInInspector]
  61. [SerializeField]
  62. public List<Material> materials = new();
  63. [Tooltip("Keep track of materials using outlines and automatically delete this feature if no materials use it." +
  64. "If disabled, you must manually remove this feature when no materials use it.")]
  65. [HideInInspector]
  66. [SerializeField]
  67. public bool autoReferenceMaterials = true;
  68. public override void Create() {
  69. #if UNITY_2022_3_OR_NEWER
  70. settings.overrideMode = RenderObjectsSettings.OverrideMaterialMode.Shader;
  71. settings.overrideShader = Shader.Find("FlatKit/Stylized Surface");
  72. settings.overrideShaderPassIndex = 1;
  73. #endif
  74. settings.filterSettings.LayerMask = -1;
  75. settings.filterSettings.PassNames = new[] { "Outline" };
  76. if (autoReferenceMaterials) {
  77. // Remove any invalid materials.
  78. materials.RemoveAll(m => m == null);
  79. } else {
  80. materials.Clear();
  81. }
  82. base.Create();
  83. }
  84. // Returns true if there are any materials using this feature.
  85. [MustUseReturnValue]
  86. public bool RegisterMaterial(Material material, bool active) {
  87. if (!autoReferenceMaterials) return true;
  88. if (active) {
  89. if (!materials.Contains(material)) {
  90. materials.Add(material);
  91. }
  92. } else {
  93. materials.Remove(material);
  94. }
  95. return materials.Count > 0;
  96. }
  97. }
  98. }